package com.example.phonerefresh;

import android.graphics.drawable.Drawable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.phonerefresh.fragment.FirstFragment;
import com.example.phonerefresh.fragment.FoundFragment;
import com.example.phonerefresh.fragment.MeFragment;
import com.example.phonerefresh.fragment.PhoneFragment;
import com.example.phonerefresh.view.util.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        float width = metrics.widthPixels * metrics.density;
        float height = metrics.heightPixels * metrics.density;
        DisplayUtil.sp2dp(this,height);

        initView();
    }

    private void initView() {
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

        if (toolbar != null){
            setSupportActionBar(toolbar);
        }
        MainPagerAdapter mAdapter = new  MainPagerAdapter(getSupportFragmentManager());
        mAdapter.addFragment(new FoundFragment().newInstance(), "Found");
        mAdapter.addFragment(new PhoneFragment().newInstance(), "Phone");
        mAdapter.addFragment(new FirstFragment().newInstance(), "First");
        mAdapter.addFragment(new MeFragment().newInstance(), "Me");

        viewPager.setAdapter(mAdapter);
        tabLayout.setupWithViewPager(viewPager);

        for (int i = 0; i < tabLayout.getTabCount(); i++){
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(mAdapter.getTabView(i));
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Toast.makeText(MainActivity.this,"Selected Page" + position,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public class MainPagerAdapter extends FragmentPagerAdapter{

        private List<Fragment> fragmentList = new ArrayList<Fragment>();
        private List<String>   titleList = new ArrayList<>();

        private int[] imageResId = new int[]{R.mipmap.facebook_logo_32,R.mipmap.facebook_logo_32,R.mipmap.facebook_logo_32,R.mipmap.facebook_logo_32};
        private int[] drawImagesId = new int[]{R.drawable.select_tab_layout,R.drawable.select_tab_layout,R.drawable.select_tab_layout,R.drawable.select_tab_layout};

        public MainPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
              return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Drawable image = getResources().getDrawable(imageResId[position]);
            image.setBounds(0,0,image.getIntrinsicWidth(),image.getIntrinsicHeight());
            SpannableString sb = new SpannableString("" + titleList.get(position));
            ImageSpan is = new ImageSpan(image,ImageSpan.ALIGN_BASELINE);
            sb.setSpan(is,0,1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
              return sb;
           }

        public View getTabView(int position) {
            View view = getLayoutInflater().inflate(R.layout.view_item_tablayout, null);
            TextView tv = (TextView) view.findViewById(R.id.tab_item_txt);
            tv.setText(titleList.get(position));
            ImageView image = (ImageView)view.findViewById(R.id.tabImage);
            image.setImageResource(drawImagesId[position]);
            return view;
        }

        public void addFragment(Fragment fragment, String titles) {
            fragmentList.add(fragment);
            titleList.add(titles);
        }
    }
}
